package com.atguigu.tingshu.order.startegy;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

//策略实现类接口
public interface OrderTradeStrategy {
    //定义业务逻辑抽象方法
    public abstract OrderInfoVo trade(TradeVo tradeVo);
    //获取策略实现类对应的itemType
    public abstract String  getItemType();

    //给orderinfoVo对象设置其他数据
   default public void setOtherDataToOrderInfoVo(OrderInfoVo orderInfoVo, StringRedisTemplate stringRedisTemplate){
       //生成uuid作为交易号
       String tradeNo = UUID.randomUUID().toString().replace("-", "");
       orderInfoVo.setTradeNo(tradeNo);
       orderInfoVo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
       //获取时间戳
       long timestamp = SignHelper.getTimestamp();
       orderInfoVo.setTimestamp(timestamp);
       String orderInfoVoStr = JSON.toJSONString(orderInfoVo);
       Map<String, Object> orderInfoMap = JSON.parseObject(orderInfoVoStr, Map.class);
       String sign = SignHelper.getSign(orderInfoMap);
       orderInfoVo.setSign(sign);

       stringRedisTemplate.opsForValue().set(RedisConstant.ORDER_TRADE_NO_PREFIX + tradeNo, tradeNo,5, TimeUnit.SECONDS);
   }
}
